/** init domain config */
import './config'

import Vue from 'vue'
import App from './App.vue'
import Storage from 'vue-ls'
import router from './router'
import store from './store/'
import { VueAxios } from "@/utils/request"

require('@jeecg/antd-online-mini')
require('@jeecg/antd-online-mini/dist/OnlineForm.css')

import Antd, { version } from 'ant-design-vue'
console.log('ant-design-vue version:', version)

import Viser from 'viser-vue'
import 'ant-design-vue/dist/antd.less';  // or 'ant-design-vue/dist/antd.less'

import '@/permission' // permission control
import '@/utils/filter' // base filter
import Print from 'vue-print-nb-jeecg'
/*import '@babel/polyfill'*/
import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
import SSO from '@/cas/sso.js'
import {
  ACCESS_TOKEN,
  DEFAULT_COLOR,
  DEFAULT_THEME,
  DEFAULT_LAYOUT_MODE,
  DEFAULT_COLOR_WEAK,
  SIDEBAR_TYPE,
  DEFAULT_FIXED_HEADER,
  DEFAULT_FIXED_HEADER_HIDDEN,
  DEFAULT_FIXED_SIDEMENU,
  DEFAULT_CONTENT_WIDTH_TYPE,
  DEFAULT_MULTI_PAGE
} from "@/store/mutation-types"
import config from '@/defaultSettings'

import JDictSelectTag from './components/dict/index.js'
import hasPermission from '@/utils/hasPermission'
import vueBus from '@/utils/vueBus';
import JeecgComponents from '@/components/jeecg/index'
import '@/assets/less/JAreaLinkage.less'
import VueAreaLinkage from 'vue-area-linkage'
import '@/components/jeecg/JVxeTable/install'
import '@/components/JVxeCells/install'
//表单验证
import { rules } from '@/utils/rules'
import JSelectMultiUser from './components/jeecgbiz/JSelectMultiUser'
import JSelectMultiEmployee from './components/jeecgbiz/JSelectMultiEmployee'
import JSelectUserByDep from './components/jeecgbiz/JSelectUserByDep'
import JSelectDepart from './components/jeecgbiz/JSelectDepart'
import JSelectPosition from './components/jeecgbiz/JSelectPosition'
import JSelectRole from './components/jeecgbiz/JSelectRole'
import JSelectEmployeeModal from './components/jeecgbiz/JSelectEmployeeModal'
import JSelectCustomerModal from './components/jeecgbiz/JSelectCustomerModal'
import JSelectProjectModal from './components/jeecgbiz/JSelectProjectModal'
import JSelectWarehouse from './components/jeecgbiz/JSelectWarehouse'
import JSelectMaterial from './components/jeecgbiz/JSelectMaterial'
Vue.prototype.rules = rules
Vue.config.productionTip = false
Vue.use(Storage, config.storageOptions)
Vue.use(Antd)
Vue.use(VueAxios, router)
Vue.use(Viser)
Vue.use(hasPermission)
Vue.use(JDictSelectTag)
Vue.use(Print)
Vue.use(preview)
Vue.use(vueBus);
Vue.use(JeecgComponents);
Vue.use(VueAreaLinkage);
Vue.component('JSelectMultiUser', JSelectMultiUser)
Vue.component('JSelectMultiEmployee', JSelectMultiEmployee)
Vue.component('JSelectUserByDep', JSelectUserByDep)
Vue.component('JSelectDepart', JSelectDepart)
Vue.component('JSelectPosition', JSelectPosition)
Vue.component('JSelectRole', JSelectRole)
Vue.component('JSelectEmployeeModal', JSelectEmployeeModal)
Vue.component('JSelectCustomerModal', JSelectCustomerModal)
Vue.component('JSelectProjectModal', JSelectProjectModal)
Vue.component('JSelectWarehouse', JSelectWarehouse)
Vue.component('JSelectMaterial', JSelectMaterial)

// 添加全局白屏检测
const detectWhiteScreen = () => {
  console.log('执行白屏检测...');
  // 检查页面是否已经渲染
  const appElement = document.getElementById('app');
  
  // 添加白屏检测标记，避免重复刷新
  const whiteScreenDetected = Vue.ls.get('WHITE_SCREEN_DETECTED');
  const currentTime = new Date().getTime();
  const lastRefreshTime = Vue.ls.get('LAST_REFRESH_TIME') || 0;
  
  // 如果在30秒内已经刷新过，则不再触发刷新
  const refreshCooldown = 30000; // 30秒冷却时间
  
  if (appElement && (!appElement.children || appElement.children.length === 0)) {
    console.warn('检测到可能的白屏问题...');
    // 检查是否有token，确保不在登录页面刷新，且未在冷却期内
    if (Vue.ls.get(ACCESS_TOKEN) && 
        window.location.pathname !== '/user/login' && 
        !whiteScreenDetected && 
        (currentTime - lastRefreshTime > refreshCooldown)) {
      
      console.log('检测到有效登录状态，执行页面刷新');
      // 设置白屏检测标记，防止重复刷新
      Vue.ls.set('WHITE_SCREEN_DETECTED', true, refreshCooldown);
      Vue.ls.set('LAST_REFRESH_TIME', currentTime, 24 * 60 * 60 * 1000); // 保存24小时
      
      // 延迟刷新，给予页面额外的加载时间
      setTimeout(() => {
        // 再次检查是否仍然是白屏
        const appElementRecheck = document.getElementById('app');
        if (appElementRecheck && (!appElementRecheck.children || appElementRecheck.children.length === 0)) {
          window.location.reload();
        } else {
          // 如果已经有内容了，取消白屏标记
          Vue.ls.remove('WHITE_SCREEN_DETECTED');
          console.log('页面已恢复正常，取消刷新');
        }
      }, 3000); // 延迟3秒再检查
    } else if (whiteScreenDetected) {
      console.log('白屏问题已被检测，等待刷新冷却期结束');
    } else if (currentTime - lastRefreshTime <= refreshCooldown) {
      console.log('页面在冷却期内，跳过刷新', (currentTime - lastRefreshTime) / 1000, '秒前已刷新');
    }
  } else {
    console.log('页面内容检查完成，渲染正常');
    // 如果页面正常，清除白屏检测标记
    if (whiteScreenDetected) {
      Vue.ls.remove('WHITE_SCREEN_DETECTED');
    }
  }
};

SSO.init(() => {
  main()
})
function main() {
  new Vue({
    router,
    store,
    mounted () {
      store.commit('SET_SIDEBAR_TYPE', Vue.ls.get(SIDEBAR_TYPE, true))
      store.commit('TOGGLE_THEME', Vue.ls.get(DEFAULT_THEME, config.navTheme))
      store.commit('TOGGLE_LAYOUT_MODE', Vue.ls.get(DEFAULT_LAYOUT_MODE, config.layout))
      store.commit('TOGGLE_FIXED_HEADER', Vue.ls.get(DEFAULT_FIXED_HEADER, config.fixedHeader))
      store.commit('TOGGLE_FIXED_SIDERBAR', Vue.ls.get(DEFAULT_FIXED_SIDEMENU, config.fixSiderbar))
      store.commit('TOGGLE_CONTENT_WIDTH', Vue.ls.get(DEFAULT_CONTENT_WIDTH_TYPE, config.contentWidth))
      store.commit('TOGGLE_FIXED_HEADER_HIDDEN', Vue.ls.get(DEFAULT_FIXED_HEADER_HIDDEN, config.autoHideHeader))
      store.commit('TOGGLE_WEAK', Vue.ls.get(DEFAULT_COLOR_WEAK, config.colorWeak))
      store.commit('TOGGLE_COLOR', Vue.ls.get(DEFAULT_COLOR, config.primaryColor))
      store.commit('SET_TOKEN', Vue.ls.get(ACCESS_TOKEN))
      store.commit('SET_MULTI_PAGE',Vue.ls.get(DEFAULT_MULTI_PAGE,config.multipage))
      
      // 延迟执行白屏检测，确保有足够的时间渲染
      setTimeout(detectWhiteScreen, 2000);
    },
    render: h => h(App)
  }).$mount('#app')
}